Skip to content

Fix listener leaks from removed browsers, viewports and tracks - #2124

Open
jrobinso wants to merge 4 commits into
masterfrom
fix-viewport-listener-leaks
Open

jrobinso wants to merge 4 commits into
masterfrom
fix-viewport-listener-leaks

Conversation

@jrobinso

@jrobinso jrobinso commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2122 and #2123, plus the similar leaks found by auditing the other listener registrations.

Reported issues

Found in the audit

  • Loading a session did not dispose the old tracks, leaving their handlers on the browser. cleanHouseForSession now calls trackView.dispose().
  • Feature tracks registered a trackdragend handler that was never removed. The listener is gone; the browser calls track.trackDragEnd() when a pan ends.
  • Seg tracks registered a trackdragend handler that set a flag nothing reads. Removed.
  • removeTrackScrollMouseHandlers and removeTrackDragMouseHandlers each removed a handler under the wrong event or handler name, so the listener stayed.

Disposal during async work

Disposing a viewport clears its properties, and a locus change or session load can now land while a load, a redraw or a pan is in progress. Viewport.dispose() sets a disposed flag, and:

  • Loads and popups stop. The flag is checked after awaits in TrackViewport.loadFeatures, IdeogramViewport.loadFeatures, handleTrackClick, the click-popup timer and TrackView.getInViewFeatures.
  • Obsolete updates are abandoned. TrackView.updateViews and the group-autoscale pass of Browser.updateViews stop if a viewport they were loading for was replaced. Otherwise they would autoscale without its features and apply the 0–100 default range over the new locus. Every path that replaces viewports (locus change, closing or selecting a multi-locus panel, session or genome load) ends with its own update, which runs against the new viewports.
  • A removed track does not abandon its group's update. Removing one track of an autoscale group disposes its viewports too, but nothing updates the rest of the group afterwards, so that update carries on without it.
  • A pan or scroll in progress is dropped. Browser.clearViewportInteraction() clears the drag state before viewports are disposed, since it refers to one of them.

Behaviour change

trackDragEnd() applies to all feature tracks; the old listener skipped snp and FusionJuncSpan tracks, which now get one extra repaint when a pan ends.

Testing

Checked in Chrome against the source, with BAM, BED, seg, and group- and self-autoscaled wig tracks, counting listeners on document, the column container and the browser's event emitter:

  • Counts are unchanged after repeated searches, return to their starting values after repeated load/remove of tracks, and are stable across repeated session loads.
  • The browser's document listeners are gone after removeBrowser.
  • Overlapping session loads, single- and multi-locus searches across chromosomes, and redraws gave no rejections or console errors, and the autoscaled data ranges afterwards matched the starting ones.
  • A search during a simulated pan cleared the pan state, and the mouse move and release that followed raised no errors.

Not done: heap measurements with forced GC, a regression test, and removing a group-autoscale track while its group is loading. The stale 0–100 range and the ideogram error were fixed from reading the code, not reproduced first. The related node tests pass; the full suite was last run after the first commit only (314 passing, 6 network timeouts).

🤖 Generated with Claude Code

jrobinso and others added 3 commits November 13, 2026 20:01
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Viewport disposal races can still cause feature-loading and mouse-interaction errors, while obsolete updates can overwrite current autoscale ranges.

4 open issues
What changed in this PR

This PR addresses listener leaks in igv.js when browsers, tracks, or viewports are removed, including the search-related leaks reported in #2122 and #2123.

Changes:

  • Dispose replaced viewports and old session tracks, and correct listener removal.
  • Replace internal drag-end listeners with a direct feature-track callback.
  • Add disposal checks around asynchronous loading and popup handling.
File Description
js/​viewport.js Marks disposed viewports for asynchronous callers.
js/​trackViewport.js Guards loading and popup handling against disposal.
js/​trackView.js Disposes discarded viewports and fixes mouse-listener cleanup.
js/​feature/​segTrack.js Removes an unused drag-end listener and flag.
js/​feature/​featureTrack.js Replaces the drag-end listener with a repaint callback.
js/​browser.js Disposes session tracks, fixes keyboard cleanup, and invokes drag-end callbacks.

Review effort: Balanced

Comment thread js/browser.js
Comment on lines 1588 to 1589
const inViewFeatures = await Promise.all(trackViews.map(trackView => trackView.getInViewFeatures()))
const dataRange = doAutoscale(inViewFeatures.flat())
Comment thread js/trackView.js Outdated
if (this.disposed) return // Track was removed during load

// Viewports replaced during the load (a locus change, a closed multi-locus panel) have been disposed of
visibleViewports = visibleViewports.filter(viewport => !viewport.disposed)
Comment thread js/trackView.js
disposeViewports() {
for (let viewport of this.viewports) {
viewport.viewportElement.remove()
viewport.dispose()
Comment thread js/trackView.js
disposeViewports() {
for (let viewport of this.viewports) {
viewport.viewportElement.remove()
viewport.dispose()
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

removeKeyboardHandler adds the keyup listener instead of removing it (removed browsers leak)

2 participants